package com.clinic.dao.imp;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.clinic.dao.interfaces.Clinic_dao;
import com.clinic.domain.Clinic;
import com.clinic.util.Util_imp;

public class Clinic_imp implements Clinic_dao {

	SessionFactory session_factory = null;

	public Clinic_imp() {
		session_factory = Util_imp.get_sessionfactory();
	}

	@Override
	public int create_clinic(Clinic c) {
		System.out.println("Enter - create_clinic");
		Session session = session_factory.openSession();
		Transaction tx = null;
		int cc_obj_id = 0;
		try {
			tx = session.beginTransaction();
			cc_obj_id = (Integer) session.save(c);
			tx.commit();
			System.out.println("New Clinic Created with ID: " + cc_obj_id);
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Create Clinic Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - create_clinic");
		return cc_obj_id;
	}

	@Override
	public void update_clinic(Clinic cc) {
		System.out.println("Enter - update_clinic");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(cc);
			tx.commit();
			System.out.println("Clinic Object Updated");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Clinic Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - update_clinic");
	}

	@Override
	public void delete_clinic(int cc_obj_id) {
		System.out.println("Enter - delete_clinic");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Clinic c = (Clinic) session.get(Clinic.class, cc_obj_id);
			session.delete(c);
			tx.commit();
			System.out.println("Clinic with ID: " + cc_obj_id + " Deleted");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Clinic Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - delete_clinic");
	}

	@Override
	public Clinic get_clinic(int cc_obj_id) {
		System.out.println("Enter - get_clinic");
		Session session = session_factory.openSession();
		Transaction tx = null;
		Clinic c = null;
		try {
			tx = session.beginTransaction();
			c = (Clinic) session.createQuery("from Clinic where pt_obj_id=:id")
					.setParameter("id", cc_obj_id).uniqueResult();
			tx.commit();
			System.out.println("Returned Object Name: " + c.getName());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_clinic");
		return c;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Clinic> get_all_clinics() {
		System.out.println("Enter - get_all_clinics");
		Session session = session_factory.openSession();
		List<Clinic> l = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			l = session.createQuery("from Clinic").list();
			tx.commit();
			System.out.println("Number of Clinic returned is: " + l.size());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_all_clinics");
		return l;
	}

	@Override
	public Clinic persistOrMerge(Clinic c) {
		return (Clinic) session_factory.getCurrentSession().merge(c);
	}
	
	@Override
	public int getsize(){
		System.out.println("Enter - getsize");
		Session session = session_factory.openSession();
		int size = 0;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			size = ((Long) session.createQuery("select count(*) from Clinic").uniqueResult()).intValue();
			tx.commit();
			System.out.println("Count(*) Clinic: " + size);
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - getsize");
		return size;
	}
}